﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PortalCurriculo.WebUI.UserControls
{
    public partial class CadastroBasico : UserControl
    {
        private const string Id = "Id";
        private const string Ativo = "Ativo";
        private const string Descricao = "Descricao";
        private const string Nome = "Nome";

        public Type EntityType { get; set; }

        private bool HasName
        {
            get { return EntityType.GetProperty(Nome) != null; }
        }

        private bool HasActive
        {
            get { return EntityType.GetProperty(Ativo) != null; }
        }

        private bool HasDescription
        {
            get { return EntityType.GetProperty(Descricao) != null; }
        }

        private static List<object> Lista { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;

            PreencherTela();
        }

        public void PreencherTela()
        {
            pnlNome.Visible = HasName;

            MethodInfo methodInfo =
                EntityType.GetMethod("SelectAllActive").MakeGenericMethod(new List<Type> {EntityType}.ToArray());

            Lista = new List<object>();
            Lista.AddRange((IEnumerable<object>) methodInfo.Invoke(Activator.CreateInstance(EntityType), null));
            PreencherListView();
        }

        private void PreencherListView()
        {
            listView.DataSource = Lista;
            listView.DataBind();
        }

        protected void listView_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            var item = (ListViewDataItem) e.Item;
            object dataItem = item.DataItem;

            if (HasDescription)
                ((Label) item.FindControl("lblDescricao")).Text =
                    dataItem.GetType().GetProperty(Descricao).GetValue(dataItem, null).ToString();
            if (HasName)
            {
                var nome = ((Label) item.FindControl("lblNome"));

                nome.Text = dataItem.GetType().GetProperty(Nome).GetValue(dataItem, null).ToString();
                nome.Visible = true;
            }

            string id = dataItem.GetType().GetProperty(Id).GetValue(dataItem, null).ToString();

            ((ImageButton) item.FindControl("imbEditar")).Attributes.Add(Id, id);
            ((ImageButton) item.FindControl("imbExcluir")).Attributes.Add(Id, id);
        }

        protected void imbExcluir_Click(object sender, ImageClickEventArgs e)
        {
            int id = int.Parse(((ImageButton) sender).Attributes["Id"]);

            object item = (from itemLinq in Lista
                           where itemLinq.GetType().GetProperty(Id).GetValue(itemLinq, null).Equals(id)
                           select itemLinq).First();

            Lista.Remove(item);

            MethodInfo methodInfo =
                EntityType.GetMethod("Inative").MakeGenericMethod(new List<Type> {EntityType}.ToArray());
            methodInfo.Invoke(item, null);

            PreencherListView();
        }

        protected void lnkAdicionar_Click(object sender, EventArgs e)
        {
            Popup.Show();
            Popup.ClearFields();
            pnlNome.Visible = HasName;
        }

        protected void lnkSalvar_Click(object sender, EventArgs e)
        {
            object entity = Activator.CreateInstance(EntityType);

            if (HasDescription)
                entity.GetType().GetProperty(Descricao).SetValue(entity, txtDescricao.Text, null);
            if (HasActive)
                entity.GetType().GetProperty(Ativo).SetValue(entity, true, null);
            if (HasName)
                entity.GetType().GetProperty(Nome).SetValue(entity, txtNome.Text, null);

            entity.GetType().GetMethod("Save").Invoke(entity, null);

            Popup.Close();

            Lista.Add(entity);
            PreencherListView();
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Popup.Show();
            Popup.ClearFields();
            pnlNome.Visible = HasName;
        }
    }
}